﻿Imports System.Windows.Forms

Public Class dbConfig
    Dim integratedSecurityDSN As String = "Data Source={0};Initial Catalog={1};Integrated Security=True"
    Dim userPassDSN As String = "Data Source={0};Initial Catalog={1};User Id={2};Password={3};"
    Const DEFAULT_DSN As String = "Data Source=FITORIA-LAPTOP\SQLEXPRESS;Initial Catalog=fichasclinicas;Integrated Security=True"
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If testDSN() = True Then
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        Else
            MsgBox("Por favor revise los parametros hay algun error", MsgBoxStyle.Critical, "Error de Conexion")
            Exit Sub
        End If
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub seguridadIntegrada_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles seguridadIntegrada.Click
        If seguridadIntegrada.CheckState = CheckState.Checked Then
            Me.txtPassword.Enabled = False
            Me.txtUser.Enabled = False
            Me.lblContrasena.Enabled = False
            Me.lblUsuario.Enabled = False
        Else
            Me.txtPassword.Enabled = False
            Me.txtUser.Enabled = False
            Me.lblContrasena.Enabled = False
            Me.lblUsuario.Enabled = False
        End If
    End Sub

    Private Sub saveDSN()
        If seguridadIntegrada.CheckState = CheckState.Checked Then
            Config.Default.DSN = String.Format(integratedSecurityDSN, Me.txtServidor.Text, _
                                               Me.txtBaseDeDatos.Text)
            Config.Default.Database = Me.txtBaseDeDatos.Text
            Config.Default.Server = Me.txtServidor.Text
            Config.Default.Save()
        Else
            Config.Default.DSN = String.Format(userPassDSN, Me.txtServidor.Text, _
                                              Me.txtBaseDeDatos.Text, Me.txtUser.Text, Me.txtPassword.Text)
            Config.Default.Database = Me.txtBaseDeDatos.Text
            Config.Default.Server = Me.txtServidor.Text
            Config.Default.Username = Me.txtUser.Text
            Config.Default.Password = Me.txtPassword.Text
            Config.Default.Save()
        End If
    End Sub
    Private Function testDSN() As Boolean
        Dim con As New Conexion
        If seguridadIntegrada.CheckState = CheckState.Checked Then
            Config.Default.DSN = String.Format(integratedSecurityDSN, Me.txtServidor.Text, _
                                               Me.txtBaseDeDatos.Text)
            Config.Default.Database = Me.txtBaseDeDatos.Text
            Config.Default.Server = Me.txtServidor.Text
        Else
            Config.Default.DSN = String.Format(userPassDSN, Me.txtServidor.Text, _
                                              Me.txtBaseDeDatos.Text, Me.txtUser.Text, Me.txtPassword.Text)
            Config.Default.Database = Me.txtBaseDeDatos.Text
            Config.Default.Server = Me.txtServidor.Text
            Config.Default.Username = Me.txtUser.Text
            Config.Default.Password = Me.txtPassword.Text
        End If
        If con.Conectar = True Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub btnProbar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProbar.Click
        If testDSN() = True Then
            MsgBox("Los parametros estan bien", MsgBoxStyle.Information, "Conexion Exitosa")
        Else
            MsgBox("Por favor revise los parametros hay algun error", MsgBoxStyle.Critical, "Error de Conexion")
        End If
    End Sub
End Class
